<?php
require_once 'prepend.inc.php';
require_once 'data_classes/Articlecategory.class.php';
require_once 'ThirdParty/AmazonInterface.php';
include_once 'Panel/pnlHeader.php';


class AmazonSearch extends QForm
{
	protected $lstSearchIndex;
	protected $txtSearchTerm;
	protected $btnSearch;
	protected $dtrAmazon;
	protected $imgAddLink;
	protected $amazonResult;
	protected $objMyAssets;
	protected $rdbSearchIndex;
	protected $lstPowerLanguage;
	protected $lstPowerGenre;
	protected $btnNext;
	protected $btnPrevious;
	protected $intCurrentPage = 1;
	protected $objWaitIcon;
	protected $pnlHeader;

	protected function Form_Run()
	{
		QApplication::CheckRemoteAdmin();
		QApplication::$LoadedPage = 'Search and Add Assets';
	}

	protected function Form_Create()
	{
		
		$this->objDefaultWaitIcon = new QWaitIcon($this);
		$this->lstSearchIndex_Create();
		$this->txtSearchTerm_Create();
		$this->btnSearch_Create();
		$this->imgAddLink_Create();
		$this->dtrAmazon_Create();
		$this->rdbSearchIndex_Create();
		$this->btnNext_Create();
		$this->btnPrevious_Create();
		$this->pnlHeader_Create();
	}

	protected function pnlHeader_Create()
	{
		$this->pnlHeader = new pnlHeader($this, 'CloseEditPane');
	}
	protected function btnNext_Create()
	{
		$this->btnNext = new QLinkButton($this);
		$this->btnNext->Text = 'Next Page';
		$this->btnNext->AddAction(new QClickEvent(), new QServerAction('btnNext_Click',$this->objDefaultWaitIcon));
	}

	protected function btnPrevious_Create()
	{
		$this->btnPrevious = new QLinkButton($this);
		$this->btnPrevious->Text = 'Previous Page';
		$this->btnPrevious->AddAction(new QClickEvent(), new QServerAction('btnPrevious_Click',$this->objDefaultWaitIcon));
	}

	protected function btnPrevious_Click()
	{
		if($this->intCurrentPage == 1)
		{
			$this->intCurrentPage  = 1;
		}
		else
		{
			$this->intCurrentPage  = $this->intCurrentPage - 1;
				
		}

		$this->dtrAmazon_Bind();
	}

	protected function btnNext_Click()
	{
		if($this->intCurrentPage == 250)
		{
			$this->intCurrentPage = 1;
		}
		else
		{
			$this->intCurrentPage  = $this->intCurrentPage + 1;
		}

		$this->dtrAmazon_Bind();

	}
	protected function rdbSearchIndex_Create()
	{
		$this->rdbSearchIndex = new QRadioButtonList($this);
		$this->rdbSearchIndex->AddItem(new QListItem('Books','B',true));
		$this->rdbSearchIndex->AddItem(new QListItem('Movies','M',false));
		$this->rdbSearchIndex->AddItem(new QListItem('Music','C',false));
		$this->rdbSearchIndex->RepeatDirection = QRepeatDirection::Horizontal;
		$this->rdbSearchIndex->RepeatColumns = 3;
	}

	protected function dtrAmazon_Create()
	{
		$this->dtrAmazon = new QDataRepeater($this);
		$this->dtrAmazon->Paginator = new QPaginator($this);
		$this->dtrAmazon->ItemsPerPage = 6;

		$this->imgAddLink = new QImageButton($this->dtrAmazon);
		$this->imgAddLink->ImageUrl = '../assets/images/48x48/add.png';
		$this->imgAddLink->AddAction(new QClickEvent(),new QServerAction('AddClicked'));

                
		$this->dtrAmazon->UseAjax = false;

		$this->dtrAmazon->Template = 'amazonresult.tpl.php';
		$this->dtrAmazon->SetDataBinder('dtrAmazon_Bind');

	}

	protected function dtrAmazon_Bind()
	{
		switch($this->rdbSearchIndex->SelectedValue)
		{
			case 'B':
				$option = 'Books';
				break;
			case 'M':
				$option = 'DVD';
				break;
			case 'C':
				$option = 'Music';
				break;
			default:
				$option = 'All';
				break;
		}
		
		if($_SESSION['User'] <> '')
		{
			if($this->txtSearchTerm->Text <> '')
			{
				$objAmazon = new AmazonInterface();
				$objAmazonResult = $objAmazon->SearchAmazon($option,$this->txtSearchTerm->Text,$this->intCurrentPage);
				$this->dtrAmazon->DataSource = $objAmazonResult;
				$this->dtrAmazon->TotalItemCount = sizeof($objAmazonResult);
				$this->dtrAmazon->RemoveChildControls(true);
				
			}
		}
	}

	private function txtSearchTerm_Create()
	{
		$this->txtSearchTerm = new QTextBox($this);
	}
	private function lstSearchIndex_Create()
	{
		$this->lstSearchIndex = new QListBox($this);
		$this->lstSearchIndex->Name = QApplication::Translate('Search Index');
		$this->lstSearchIndex->AddItem(QApplication::Translate('- Select One -'), null);
		
		$objArticleCategoryObjectArray = Articlecategory::LoadAll();
		
		if ($objArticleCategoryObjectArray)
		foreach ($objArticleCategoryObjectArray as $objArticleCategoryObject)
		{
			$objListItem = new QListItem($objArticleCategoryObject->__toString(), $objArticleCategoryObject->Id);
			$this->lstSearchIndex->AddItem($objListItem,$objArticleCategoryObject->Id);
		}
	}

	private function btnSearch_Create()
	{
		$this->btnSearch = new QButton($this);
		$this->btnSearch->Text = 'Search Amazon';
		$this->btnSearch->AddAction(new QClickEvent(), new QServerAction('btnSearch_Click'));
	}

	protected function btnSearch_Click()
	{
/*		if($_SESSION['User'] <> '')
		{
			$this->dtrAmazon_Bind();
		}
		else
		{
			QApplication::DisplayAlert('Please Login to use this feature');
			QApplication::ExecuteJavaScript('document.location = "index.php";');
		}*/
		$this->dtrAmazon_Bind();
	}

	function imgAddLink_Create()
	{
		$this->imgAddLink = new QImageButton($this);
		$this->imgAddLink->ImageUrl = '../assets/images/48x48/add.png';
		$this->imgAddLink->AddAction(new QClickEvent(),new QServerAction('AddClicked'));

	}

	function imgAddLink_Render()
	{
		//$this->imgAddLink->Render();
	}

	function AddClicked($strFormId,$strControlId,$strParameter)
	{
		$arrParameter = explode('~',$strParameter);
		$arrPrepare['ASIN'] = $arrParameter[0];
		$arrPrepare['Title'] = $arrParameter[1];
		$arrPrepare['ProductGroup'] = $arrParameter[2];
		$arrPrepare['Image'] = $arrParameter[3];
		$arrPrepare['DetailPageURL'] = $arrParameter[4];

		//Fill the new MyAssets table which is MyISAM based and better

		$this->objMyAssets = new Myassets();
		$this->objMyAssets->Asin = $arrPrepare['ASIN'];
		$this->objMyAssets->Title = $arrPrepare['Title'];
		$this->objMyAssets->ImageURL = $arrPrepare['Image'];
		$this->objMyAssets->DetailPageURL = $arrPrepare['DetailPageURL'];
		$this->objMyAssets->ProductGroup = $arrPrepare['ProductGroup'];


		switch($arrPrepare['ProductGroup'])
		{
			case 'Book':
				//Fill MyAssets
				$this->objMyAssets->Author = $arrParameter[5];
				$this->objMyAssets->NumberOfPages = $arrParameter[7];
				$this->objMyAssets->Publisher = $arrParameter[8];
				$this->objMyAssets->Isbn = $arrParameter[10];
				break;
			case 'DVD':
				//Fill MyAssets
				$this->objMyAssets->Actor = $arrParameter[5];
				$this->objMyAssets->Director = $arrParameter[6];
				$this->objMyAssets->RunningTime = $arrParameter[7];
				break;
			case 'Music':
				//Fill MyAssets
				$this->objMyAssets->Artist = $arrParameter[5];
				$this->objMyAssets->Label = $arrParameter[6];
				$this->objMyAssets->NumberOfDiscs = $arrParameter[7];
				break;
		}
		$this->objMyAssets->Owner = $_SESSION['User'];
		$this->objMyAssets->Save();
		QApplication::DisplayAlert('"' . $this->objMyAssets->Title . '"' . ' has been added to your library');
	}

	protected function MyLibrary_Click()
	{
		QApplication::Redirect('MyLibrary.php');
	}

	protected function Home_Click()
	{
		QApplication::Redirect('index.php');
	}
}

AmazonSearch::Run('AmazonSearch');
